//
//  Mail_GetDetail_Task.m
//  EpointFrame5
//
//  Created by Pill Gong on 14-2-12.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "Mail_GetDetail_Task.h"
#import "MailInfo.h"

@implementation Mail_GetDetail_Task

- (void)execute {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *ValidateData = [EpointBizTools getToken];
    NSString *url = self.getMobileOAWebserviceURL;
    NSString *wsNamespace = NSLocalizedString(@"Epoint_DNet_WS_NameSpace", @"");
    NSString *userguid = [userDefaults objectForKey:Epoint_User_UserGuid];
    NSString *MailGuid = [params objectForKey:@"MailGuid"];
    
//    EpointWebServiceUtil *ws = [[EpointWebServiceUtil alloc]initWithUrl:url namespace:wsNamespace method:@"Mail_GetDetail"];
    
    NSString *ParasXml = @"<![CDATA[<?xml version=\"1.0\" encoding=\"gb2312\"?><paras><UserGuid>%@</UserGuid><MailGuid>%@</MailGuid></paras>]]>";
    ParasXml = [NSString stringWithFormat:ParasXml,userguid,MailGuid];
//    [ws addParameterForString:@"ValidateData" value:ValidateData];
//    [ws addParameterForString:@"ParasXml" value:ParasXml];
//    NSString *bs = ws.start;
    
//    if ([self validateXMLWS:bs]) {
//        [self exeSuccessWithData:[self getLst:bs]];
//    }
    
    if (![self isNetWorkAvailable])
    {
        [self userCache:ParasXml message:@"网络请求失败，请检查网络配置！"];
    }
    else
    {
        //请求接口
        EpointWebServiceUtil *ws = [[EpointWebServiceUtil alloc]initWithUrl:url namespace:wsNamespace method:@"Mail_GetDetail"];
        [ws addParameterForString:@"ValidateData" value:ValidateData];
        [ws addParameterForString:@"ParasXml" value:ParasXml];
        NSString *bs =  ws.start;
        
        NSMutableArray *result = [self validateXMLWS2:bs];
        NSString *msg = [result objectAtIndex:1];
        if ([[result objectAtIndex:0]isEqualToString:@"1"]) {
            [DBFrameService setCacheValue:ParasXml value:bs];
            [self exeSuccessWithData:[self getLst:bs]];
        }
        else
        {
            [self userCache:ParasXml message:msg];
        }
    }
}

-(void)userCache:(NSString *)key message:(NSString *)msg
{
    //获取缓存数据
    NSString *cacheBS = [DBFrameService getCacheValue:key];
    
    //判断是否存在缓存
    if (cacheBS.length>0) {
        [self exeSuccessWithData:[self getLst:cacheBS]];
    }
    else
    {
        [self exeFailureWithMessage:msg];
    }
}

-(NSMutableArray *)getLst:(NSString *)bs {
    NSString *xmldata = [EpointStringUtil getXMLFormatedAttributeBsWithTag:bs xmlTag:@"UserArea"];
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString:xmldata  options:0 error:nil];
    GDataXMLElement *rootElement = [doc rootElement];
    NSArray *arrays = [rootElement elementsForName:@"MailInfo"];
    NSMutableArray *listmp = [[NSMutableArray alloc]init];
    for (GDataXMLElement *arr in arrays) {
        MailInfo *mailInfo = [[MailInfo alloc] init];
        [mailInfo setContentWithArray:arr];
        [listmp addObject:mailInfo];
    }
    return listmp;
}

@end
